package com.fliangyun.nsa.nsamanage.utils;

import org.apache.commons.lang3.StringUtils;

import java.io.File;

/**
 * @author fuliangyun@139.com
 * @date 2023年05月30日
 * @description
 */
public class FileUtil {

    public static final String BIAS = "/";

    /**
     * 获取路径
     * @author fuliangyun@139.com
     * @date 2023-05-30 9:31
     * @param baseFolder 基础文件夹
     * @param path 路径
     * @return String
     */
    public static String getPath(String baseFolder, String... path){
        if (StringUtils.isBlank(baseFolder)){
            return "";
        }

        // 修正路径
        baseFolder = baseFolder.replace("\\", "/");
        while (baseFolder.contains("//")){
            baseFolder = baseFolder.replace("//", "/");
        }
        if (baseFolder.endsWith("/")){
            baseFolder = baseFolder.substring(0, baseFolder.length()-1);
        }

        if (path == null || path.length == 0){
            return baseFolder;
        }

        StringBuilder baseFolderBuilder = new StringBuilder(baseFolder);
        for (String s : path) {
            String p = s.replace("\\", "/");
            if (!p.startsWith("/")){
                baseFolderBuilder.append(BIAS).append(p);
            }else {
                baseFolderBuilder.append(p);
            }
        }
        baseFolder = baseFolderBuilder.toString();
        return baseFolder;
    }

    /**
     * 创建文件夹
     * @author fuliangyun@139.com
     * @date 2023-05-30 9:33
     * @param path 路径
     * @return boolean
     */
    public static boolean createFolder(String path){
        if (StringUtils.isBlank(path)){
            return false;
        }
        File file = new File(path);
        return file.mkdirs();
    }

}
